package com.example.blog.controller;

import com.example.blog.constant.Constant;
import com.example.blog.model.BlogInfo;
import com.example.blog.model.PageRequest;
import com.example.blog.model.PageResponse;
import com.example.blog.model.Result;
import com.example.blog.service.BlogService;
import com.example.blog.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfo> getList() {
        log.info("获取博客列表======");
        return blogService.getList();

    }


    @RequestMapping("/getListByPage")
    public PageResponse<BlogInfo> getListByPage(PageRequest pageRequest) {
        log.info("#getListByPage, pageRequest: {}",pageRequest);
        PageResponse<BlogInfo> pageResponse = blogService.getListByPage(pageRequest);
        return pageResponse;
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request) {
        log.info("getBlogDetail, blogID: {}",blogId);
        BlogInfo blogDetail = blogService.getBlogDetail(blogId);

        String token = request.getHeader(Constant.REQUEST_HEADER_TOKEN);
        Integer userId = JwtUtils.getIdByToken(token);

        if (userId != null && userId == blogDetail.getUserId()) {
            blogDetail.setIsLoginUser(true);
        }else {
            blogDetail.setIsLoginUser(false);
        }
        return blogDetail;

    }

    @RequestMapping("/add")
    public Result addBlog(@RequestBody BlogInfo blogInfo, HttpServletRequest request) {
        log.info("接受 bloginfo: {}",blogInfo);
        if (!StringUtils.hasLength(blogInfo.getTitle()) || !StringUtils.hasLength(blogInfo.getContent())) {
            return Result.fail("标题或内容为空");
        }
        String token = request.getHeader(Constant.REQUEST_HEADER_TOKEN);
        Integer id = JwtUtils.getIdByToken(token);
        blogInfo.setUserId(id);
        Boolean result = blogService.insertBlog(blogInfo);
        if (result) {
            return Result.success(null);
        }
        return Result.fail("插入失败");

    };

    @RequestMapping("/update")
    public Result update(@RequestBody BlogInfo blogInfo) {
        log.info("接收参数 blogInfo: {}",blogInfo);
        if (blogInfo == null || blogInfo.getId() == null || blogInfo.getId() <= 0
                || !StringUtils.hasLength(blogInfo.getTitle()) || !StringUtils.hasLength(blogInfo.getContent())) {
            return Result.fail("参数错误");
        }
        Integer result =  blogService.update(blogInfo);
        return Result.success(null);
    }



    @RequestMapping("/delete")
    public Result delete(Integer blogId) {
        log.info("接收参数 blogId: {}",blogId);
        if (blogId == null || blogId <= 0) {
            return Result.fail("博客id不合法");
        }
        BlogInfo blog = new BlogInfo();
        blog.setId(blogId);
        blog.setDeleteFlag(1);
        Integer result =  blogService.update(blog);
        return Result.success(null);
    }
}
